package game;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Vector;

public class VisibleBullet extends Thread implements VisibleCharacter {

	int x, y;

	boolean sense;

	int strength = 50;

	int diam = 10;

	Graphics g;

	Color color = Color.ORANGE;

	VisibleCharacter badGuy;

	Vector<VisibleCharacter> bandOfVilain;

	public VisibleBullet(Graphics g, int x, int y, boolean sense,
			VisibleCharacter badGuy) {
		this.x = x;
		this.y = y;
		this.sense = sense;
		this.g = g;
		this.badGuy = badGuy;

		g.fillOval(x, y, diam, diam);
	}

	public VisibleBullet(Graphics g, int x, int y, boolean sense,
			Vector<VisibleCharacter> bandOfVilain) {
		this.x = x;
		this.y = y;
		this.sense = sense;
		this.g = g;

		this.bandOfVilain = bandOfVilain;

		g.fillOval(x, y, diam, diam);
	}

	public void run() {

		int i = 0;
		while (true) {
			try {
				this.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (sense) {
				move(0, -3);
				if ((this.y + diam) < 0) {
					this.stop();
				}

				for (VisibleCharacter current : bandOfVilain) {

					if (testTouch(current)) {
						try {
							System.out.println(bandOfVilain.toString());
							this.attaque(current);
							this.stop();
						} catch (DeathException e) {
							bandOfVilain.remove(current);
							System.out.println("mort" + bandOfVilain.toString());
						}
					}
				}

			} else {
				move(0, 3);
				if (this.y > 500)
					this.stop();
			}

		}

	}

	public boolean testTouch(VisibleCharacter bg) {
		return ((this.x < (bg.getX() + bg.getWidth()))
				&& (this.x + this.diam >= bg.getX())
				&& (this.y < (bg.getY() + bg.getWidth())) && (this.y
				+ this.diam >= bg.getY()));
	}

	public int getWidth() {
		// TODO Auto-generated method stub
		return diam;
	}

	public int getX() {
		// TODO Auto-generated method stub
		return x;
	}

	public int getY() {
		// TODO Auto-generated method stub
		return y;
	}

	public void move(int x, int y) {
		int newX = this.x + x;
		int newY = this.y + y;
		Color tmpColor = g.getColor();

		g.setColor(color);
		g.clearRect(this.x, this.y, diam, diam);
		g.fillOval(this.x + x, this.y + y, diam, diam);
		g.setColor(tmpColor);

		this.x = newX;
		this.y = newY;
	}

	public void attaque(VisibleCharacter c) throws DeathException {
		c.takeDammage(this.strength);
	}

	public int getHp() {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getSpeed() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void takeDammage(int dammage) throws DeathException {
		// TODO Auto-generated method stub

	}

}
